#include "m3g/M3GWriter.hpp"
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <cstring>
using namespace std;
using namespace m3g;
const char* output_file_name = "simple.m3g";

int main (int argc, char** argv)
{
    cout << "Sample-02-Writer: \n";
    cout << "  writing to " << output_file_name << " ";

    M3GWriter*   writer = new M3GWriter(output_file_name);

    unsigned int header_index;
    unsigned int camera_index;
    unsigned int background_index;
    unsigned int positions_index;
    unsigned int normals_index;
    unsigned int vertex_buffer_index;
    unsigned int index_index;
    unsigned int material_index;
    unsigned int appearance_index;
    unsigned int mesh_index;
    unsigned int light_index;
    unsigned int world_index;

    // M3G ident
    writer->writeID ();

    // Section 1 has Header object only.
    M3GHeaderStruct header;
    header.version_major            = 1;
    header.version_minor            = 0;
    header.has_external_references  = false;
    header.total_file_size          = 0;  // override, auto calculated
    header.approximate_content_size = 0;  // override, auto calculated
    header.authoring_field          = "M3GReaderWriter by tueda@wolf.dog.cx";
    writer->startSection (0);
    writer->startObject  (M3G_TYPE_HEADER, &header_index);
    writer->writeHeader  (header);
    writer->endObject    ();
    writer->endSection   ();
    cout << ".";


    // Section2: start
    writer->startSection (1);  // 1=compress

    // M3G_TYPE_CAMERA
    {
        M3GObject3DStruct      obj;
        M3GTransformableStruct tra;
        M3GNodeStruct          nod;
        M3GCameraStruct        cam;
        obj.user_id                 = 0;
        obj.animation_tracks_count  = 0;
        obj.animation_tracks_index  = NULL;
        obj.user_parameter_count    = 0;
        obj.parameter_id            = NULL;
        obj.parameter_value_length  = NULL;
        obj.parameter_value         = NULL;
        tra.has_component_transform = false;
        tra.translation[0]          = 0;
        tra.translation[1]          = 0;
        tra.translation[2]          = 0;
        tra.scale[0]                = 1;
        tra.scale[1]                = 1;
        tra.scale[2]                = 1;
        tra.orientation_angle       = 0;
        tra.orientation_axis[0]     = 0;
        tra.orientation_axis[1]     = 0;
        tra.orientation_axis[2]     = 0;
        tra.has_general_transform   = true;
        float transform_value[16]   = { 0.76894957, -0.35611174, 0.53094351, 8.0493085,
                                        0         ,  0.83049530, 0.55702567, 8.4292517,
                                       -0.63930947, -0.42832461, 0.63860899, 9.4439050,
                                        0         ,  0         , 0         , 1};
        memcpy (tra.transform, transform_value, sizeof(transform_value));
        nod.enable_picking          = true;
        nod.alpha_factor            = 255;
        nod.scope                   = -1;
        nod.has_alignment           = false;
        nod.z_target                = 0;
        nod.y_target                = 0;
        nod.z_reference_index       = 0;
        nod.y_reference_index       = 0;
        cam.projection_type         = M3GCameraStruct::PERSPECTIVE;
        float matrix_value[16]      = {1, 0, 0, 0,
                                       0, 1, 0, 0,
                                       0, 0, 1, 0,
                                       0, 0, 0, 1};
        memcpy (cam.matrix, matrix_value, sizeof(matrix_value));
        cam.fovy                    = 45;
        cam.aspect_ratio            = 1.33333;
        cam.near                    = 0.1;
        cam.far                     = 100;
        writer->startObject        (M3G_TYPE_CAMERA, &camera_index);
        writer->writeObject3D      (obj);
        writer->writeTransformable (tra);
        writer->writeNode          (nod);
        writer->writeCamera        (cam);
        writer->endObject          ();
        cout << ".";
    }

    // M3G_TYPE_BACKGROUND
    {
        M3GObject3DStruct   obj;
        M3GBackgroundStruct bg;
        obj.user_id                = 0;
        obj.animation_tracks_count = 0;
        obj.animation_tracks_index = NULL;
        obj.user_parameter_count   = 0;
        obj.parameter_id           = NULL;
        obj.parameter_value_length = NULL;
        obj.parameter_value        = NULL;
        bg.background_color        = 0x000000ff;
        bg.background_image_index  = 0;
        bg.background_image_mode_x = M3GBackgroundStruct::REPEAT;
        bg.background_image_mode_y = M3GBackgroundStruct::REPEAT;
        bg.crop_x                  = 0;
        bg.crop_y                  = 0;
        bg.crop_width              = 0;
        bg.crop_height             = 0;
        bg.depth_clear_enabled     = true;
        bg.color_clear_enabled     = true;
        writer->startObject     (M3G_TYPE_BACKGROUND, &background_index);
        writer->writeObject3D   (obj);
        writer->writeBackground (bg);
        writer->endObject       ();
        cout << ".";
    }

    // M3G_TYPE_VERTEX_ARRAY
    {
        M3GObject3DStruct    obj;
        M3GVertexArrayStruct varry;
        obj.user_id                = 0;
        obj.animation_tracks_count = 0;
        obj.animation_tracks_index = NULL;
        obj.user_parameter_count   = 0;
        obj.parameter_id           = NULL;
        obj.parameter_value_length = NULL;
        obj.parameter_value        = NULL;
        varry.component_size       = 2;
        varry.component_count      = 3;
        varry.encoding             = M3GVertexArrayStruct::ENCODING_0;
        varry.vertex_count         = 24;
        varry.short_components     = new short*[24];

        varry.short_components[0]     = new short[3];
        varry.short_components[0][0]  =  32766;
        varry.short_components[0][1]  =  32766;
        varry.short_components[0][2]  = -32766;
        varry.short_components[1]     = new short[3];
        varry.short_components[1][0]  =  32766;
        varry.short_components[1][1]  = -32766;
        varry.short_components[1][2]  = -32766;
        varry.short_components[2]     = new short[3];
        varry.short_components[2][0]  = -32766;
        varry.short_components[2][1]  = -32766;
        varry.short_components[2][2]  = -32766;
        varry.short_components[3]     = new short[3];
        varry.short_components[3][0]  = -32766;
        varry.short_components[3][1]  =  32766;
        varry.short_components[3][2]  = -32766;

        varry.short_components[4]     = new short[3];
        varry.short_components[4][0]  =  32766;
        varry.short_components[4][1]  =  32766;
        varry.short_components[4][2]  =  32766;
        varry.short_components[5]     = new short[3];
        varry.short_components[5][0]  = -32766;
        varry.short_components[5][1]  =  32766;
        varry.short_components[5][2]  =  32766;
        varry.short_components[6]     = new short[3];
        varry.short_components[6][0]  = -32766;
        varry.short_components[6][1]  = -32766;
        varry.short_components[6][2]  =  32766;
        varry.short_components[7]     = new short[3];
        varry.short_components[7][0]  =  32766;
        varry.short_components[7][1]  = -32766;
        varry.short_components[7][2]  =  32766;

        varry.short_components[8]     = new short[3];
        varry.short_components[8][0]  =  32766;
        varry.short_components[8][1]  =  32766;
        varry.short_components[8][2]  = -32766;
        varry.short_components[9]     = new short[3];
        varry.short_components[9][0]  =  32766;
        varry.short_components[9][1]  =  32766;
        varry.short_components[9][2]  =  32766;
        varry.short_components[10]     = new short[3];
        varry.short_components[10][0]  =  32766;
        varry.short_components[10][1]  = -32766;
        varry.short_components[10][2]  =  32766;
        varry.short_components[11]     = new short[3];
        varry.short_components[11][0]  =  32766;
        varry.short_components[11][1]  = -32766;
        varry.short_components[11][2]  = -32766;

        varry.short_components[12]     = new short[3];
        varry.short_components[12][0]  =  32766;
        varry.short_components[12][1]  = -32766;
        varry.short_components[12][2]  = -32766;
        varry.short_components[13]     = new short[3];
        varry.short_components[13][0]  =  32766;
        varry.short_components[13][1]  = -32766;
        varry.short_components[13][2]  =  32766;
        varry.short_components[14]     = new short[3];
        varry.short_components[14][0]  = -32766;
        varry.short_components[14][1]  = -32766;
        varry.short_components[14][2]  =  32766;
        varry.short_components[15]     = new short[3];
        varry.short_components[15][0]  = -32766;
        varry.short_components[15][1]  = -32766;
        varry.short_components[15][2]  = -32766;

        varry.short_components[16]     = new short[3];
        varry.short_components[16][0]  = -32766;
        varry.short_components[16][1]  = -32766;
        varry.short_components[16][2]  = -32766;
        varry.short_components[17]     = new short[3];
        varry.short_components[17][0]  = -32766;
        varry.short_components[17][1]  = -32766;
        varry.short_components[17][2]  =  32766;
        varry.short_components[18]     = new short[3];
        varry.short_components[18][0]  = -32766;
        varry.short_components[18][1]  =  32766;
        varry.short_components[18][2]  =  32766;
        varry.short_components[19]     = new short[3];
        varry.short_components[19][0]  = -32766;
        varry.short_components[19][1]  =  32766;
        varry.short_components[19][2]  = -32766;

        varry.short_components[20]     = new short[3];
        varry.short_components[20][0]  =  32766;
        varry.short_components[20][1]  =  32766;
        varry.short_components[20][2]  =  32766;
        varry.short_components[21]     = new short[3];
        varry.short_components[21][0]  =  32766;
        varry.short_components[21][1]  =  32766;
        varry.short_components[21][2]  = -32766;
        varry.short_components[22]     = new short[3];
        varry.short_components[22][0]  = -32766;
        varry.short_components[22][1]  =  32766;
        varry.short_components[22][2]  = -32766;
        varry.short_components[23]     = new short[3];
        varry.short_components[23][0]  = -32766;
        varry.short_components[23][1]  =  32766;
        varry.short_components[23][2]  =  32766;

        writer->startObject      (M3G_TYPE_VERTEX_ARRAY, &positions_index);
        writer->writeObject3D    (obj);
        writer->writeVertexArray (varry);
        writer->endObject        ();
        cout << ".";
    }

    // M3G_TYPE_VERTEX_ARRAY
    {
        M3GObject3DStruct    obj;
        M3GVertexArrayStruct varry;
        obj.user_id                = 0;
        obj.animation_tracks_count = 0;
        obj.animation_tracks_index = NULL;
        obj.user_parameter_count   = 0;
        obj.parameter_id           = NULL;
        obj.parameter_value_length = NULL;
        obj.parameter_value        = NULL;
        varry.component_size       = 1;
        varry.component_count      = 3;
        varry.encoding             = M3GVertexArrayStruct::ENCODING_0;
        varry.vertex_count         = 24;
        varry.char_components      = new char*[24];

        varry.char_components[0]    = new char[3];
        varry.char_components[0][0] = 0;
        varry.char_components[0][1] = 0;
        varry.char_components[0][2] = -128;
        varry.char_components[1]    = new char[3];
        varry.char_components[1][0] = 0;
        varry.char_components[1][1] = 0;
        varry.char_components[1][2] = -128;
        varry.char_components[2]    = new char[3];
        varry.char_components[2][0] = 0;
        varry.char_components[2][1] = 0;
        varry.char_components[2][2] = -128;
        varry.char_components[3]    = new char[3];
        varry.char_components[3][0] = 0;
        varry.char_components[3][1] = 0;
        varry.char_components[3][2] = -128;

        varry.char_components[4]    = new char[3];
        varry.char_components[4][0] = 0;
        varry.char_components[4][1] = 0;
        varry.char_components[4][2] = 127;
        varry.char_components[5]    = new char[3];
        varry.char_components[5][0] = 0;
        varry.char_components[5][1] = 0;
        varry.char_components[5][2] = 127;
        varry.char_components[6]    = new char[3];
        varry.char_components[6][0] = 0;
        varry.char_components[6][1] = 0;
        varry.char_components[6][2] = 127;
        varry.char_components[7]    = new char[3];
        varry.char_components[7][0] = 0;
        varry.char_components[7][1] = 0;
        varry.char_components[7][2] = 127;

        varry.char_components[8]    = new char[3];
        varry.char_components[8][0] = 127;
        varry.char_components[8][1] = 0;
        varry.char_components[8][2] = 0;
        varry.char_components[9]    = new char[3];
        varry.char_components[9][0] = 127;
        varry.char_components[9][1] = 0;
        varry.char_components[9][2] = 0;
        varry.char_components[10]    = new char[3];
        varry.char_components[10][0] = 127;
        varry.char_components[10][1] = 0;
        varry.char_components[10][2] = 0;
        varry.char_components[11]    = new char[3];
        varry.char_components[11][0] = 127;
        varry.char_components[11][1] = 0;
        varry.char_components[11][2] = 0;

        varry.char_components[12]    = new char[3];
        varry.char_components[12][0] = 0;
        varry.char_components[12][1] = -128;
        varry.char_components[12][2] = 0;
        varry.char_components[13]    = new char[3];
        varry.char_components[13][0] = 0;
        varry.char_components[13][1] = -128;
        varry.char_components[13][2] = 0;
        varry.char_components[14]    = new char[3];
        varry.char_components[14][0] = 0;
        varry.char_components[14][1] = -128;
        varry.char_components[14][2] = 0;
        varry.char_components[15]    = new char[3];
        varry.char_components[15][0] = 0;
        varry.char_components[15][1] = -128;
        varry.char_components[15][2] = 0;

        varry.char_components[16]    = new char[3];
        varry.char_components[16][0] = -128;
        varry.char_components[16][1] = 0;
        varry.char_components[16][2] = 0;
        varry.char_components[17]    = new char[3];
        varry.char_components[17][0] = -128;
        varry.char_components[17][1] = 0;
        varry.char_components[17][2] = 0;
        varry.char_components[18]    = new char[3];
        varry.char_components[18][0] = -128;
        varry.char_components[18][1] = 0;
        varry.char_components[18][2] = 0;
        varry.char_components[19]    = new char[3];
        varry.char_components[19][0] = -128;
        varry.char_components[19][1] = 0;
        varry.char_components[19][2] = 0;

        varry.char_components[20]    = new char[3];
        varry.char_components[20][0] = 0;
        varry.char_components[20][1] = 127;
        varry.char_components[20][2] = 0;
        varry.char_components[21]    = new char[3];
        varry.char_components[21][0] = 0;
        varry.char_components[21][1] = 127;
        varry.char_components[21][2] = 0;
        varry.char_components[22]    = new char[3];
        varry.char_components[22][0] = 0;
        varry.char_components[22][1] = 127;
        varry.char_components[22][2] = 0;
        varry.char_components[23]    = new char[3];
        varry.char_components[23][0] = 0;
        varry.char_components[23][1] = 127;
        varry.char_components[23][2] = 0;

        writer->startObject      (M3G_TYPE_VERTEX_ARRAY, &normals_index);
        writer->writeObject3D    (obj);
        writer->writeVertexArray (varry);
        writer->endObject        ();
        cout << ".";
    }

    // M3G_TYPE_VERTEX_BUFFER
    {
        M3GObject3DStruct     obj;
        M3GVertexBufferStruct vbuf;
        obj.user_id                = 0;
        obj.animation_tracks_count = 0;
        obj.animation_tracks_index = NULL;
        obj.user_parameter_count   = 0;
        obj.parameter_id           = NULL;
        obj.parameter_value_length = NULL;
        obj.parameter_value        = NULL;
        vbuf.default_color         = 0xffffff;
        vbuf.positions_index       = positions_index;
        vbuf.position_scale        =  3.05190e-05;
        vbuf.position_bias[0]      =  5.96046e-08;
        vbuf.position_bias[1]      = -1.19209e-07;
        vbuf.position_bias[2]      = 0;
        vbuf.normals_index         = normals_index;
        vbuf.colors_index          = 0;
        vbuf.texcoord_array_count  = 0;
        writer->startObject       (M3G_TYPE_VERTEX_BUFFER, &vertex_buffer_index);
        writer->writeObject3D     (obj);
        writer->writeVertexBuffer (vbuf);
        writer->endObject         ();
        cout << ".";
    }

    // M3G_TYPE_TRIANGLE_STRIP_ARRAY
    {
        M3GObject3DStruct           obj;
        M3GIndexBufferStruct        ibuf;
        M3GTriangleStripArrayStruct tris;
        obj.user_id                = 0;
        obj.animation_tracks_count = 0;
        obj.animation_tracks_index = NULL;
        obj.user_parameter_count   = 0;
        obj.parameter_id           = NULL;
        obj.parameter_value_length = NULL;
        obj.parameter_value        = NULL;
        tris.encoding              = M3GTriangleStripArrayStruct::ENCODING_128;
        tris.start_index           = 0;
        tris.indices_count         = 24;
        tris.indices                = new unsigned int[24];
        int indices_value[24]       = {1, 2, 0, 3, 5, 6, 4, 7, 9, 10, 8, 11, 13, 14, 12, 15, 17, 18, 16, 19, 21, 22, 20, 23};
         memcpy (tris.indices, indices_value, sizeof(indices_value));
        tris.strip_lengths_count   = 6;
        tris.strip_lengths         = new unsigned int[6];
        int lengths_value[6]  = {4, 4, 4, 4, 4, 4};
        memcpy (tris.strip_lengths, lengths_value, sizeof(lengths_value));
        writer->startObject             (M3G_TYPE_TRIANGLE_STRIP_ARRAY, &index_index);
        writer->writeObject3D           (obj);
        writer->writeIndexBuffer        (ibuf);
        writer->writeTriangleStripArray (tris);
        writer->endObject               ();
        cout << ".";
    }

    // M3G_TYPE_MATERIAL
    {
        M3GObject3DStruct obj;
        M3GMaterialStruct mat;
        obj.user_id                = 0;
        obj.animation_tracks_count = 0;
        obj.animation_tracks_index = NULL;
        obj.user_parameter_count   = 0;
        obj.parameter_id           = NULL;
        obj.parameter_value_length = NULL;
        obj.parameter_value        = NULL;
        mat.ambient_color          = 0x0;
        mat.diffuse_color          = 0xffc000ff;  // 青むらさき
        mat.emissive_color         = 0x0;
        mat.specular_color         = 0x0;
        mat.shininess              = 0;
        mat.vertex_color_tracking_enabled = false;
        writer->startObject       (M3G_TYPE_MATERIAL, &material_index);
        writer->writeObject3D     (obj);
        writer->writeMaterial     (mat);
        writer->endObject         ();
        cout << ".";
    }

    // M3G_TYPE_APPEARANCE
    {
        M3GObject3DStruct   obj;
        M3GAppearanceStruct app;
        obj.user_id                = 0;
        obj.animation_tracks_count = 0;
        obj.animation_tracks_index = NULL;
        obj.user_parameter_count   = 0;
        obj.parameter_id           = NULL;
        obj.parameter_value_length = NULL;
        obj.parameter_value        = NULL;
        app.layer                  = 0;
        app.compositing_mode_index = 0;
        app.fog_index              = 0;
        app.polygon_mode_index     = 0;
        app.material_index         = material_index;
        app.textures_count         = 0;
        app.textures_index         = NULL;
        writer->startObject       (M3G_TYPE_APPEARANCE, &appearance_index);
        writer->writeObject3D     (obj);
        writer->writeAppearance   (app);
        writer->endObject         ();
        cout << ".";
    }

    // M3G_TYPE_MESH
    {
        M3GObject3DStruct      obj;
        M3GTransformableStruct tra;
        M3GNodeStruct          nod;
        M3GMeshStruct          mesh;
        obj.user_id                 = 0;
        obj.animation_tracks_count  = 0;
        obj.animation_tracks_index  = NULL;
        obj.user_parameter_count    = 0;
        obj.parameter_id            = NULL;
        obj.parameter_value_length  = NULL;
        obj.parameter_value         = NULL;
        tra.has_component_transform    = true;
        tra.translation[0]             = 0;
        tra.translation[1]             = 0;
        tra.translation[2]             = 0;
        tra.scale[0]                   = 1;
        tra.scale[1]                   = 1;
        tra.scale[2]                   = 1;
        tra.orientation_angle          = 0;
        tra.orientation_axis[0]        = 0;
        tra.orientation_axis[1]        = 0;
        tra.orientation_axis[2]        = 0;
        tra.has_general_transform      = true;
        float transform_value[16]      = {1, 0, 0, 0,
                                          0, 1, 0, 0,
                                          0, 0, 1, 0,
                                          0, 0, 0, 1};
        memcpy (tra.transform, transform_value, sizeof(transform_value));
        nod.enable_rendering  = true;
        nod.enable_picking    = true;
        nod.alpha_factor      = 255;
        nod.scope             = -1;
        nod.has_alignment     = false;
        nod.z_target          = 0;
        nod.y_target          = 0;
        nod.z_reference_index = 0;
        nod.y_reference_index = 0;
        mesh.vertex_buffer_index   = vertex_buffer_index;
        mesh.submesh_count         = 1;
        mesh.index_buffer_index    = new unsigned int[1];
        mesh.index_buffer_index[0] = index_index;
        mesh.appearance_index      = new unsigned int[1];
        mesh.appearance_index[0]   = appearance_index;
        writer->startObject       (M3G_TYPE_MESH, &mesh_index);
        writer->writeObject3D     (obj);
        writer->writeTransformable(tra);
        writer->writeNode         (nod);
        writer->writeMesh         (mesh);
        writer->endObject         ();
        cout << ".";
    }

    // M3G_TYPE_LIGHT
    {
        M3GObject3DStruct      obj;
        M3GTransformableStruct tra;
        M3GNodeStruct          nod;
        M3GLightStruct         lgh;
        obj.user_id                 = 0;
        obj.animation_tracks_count  = 0;
        obj.animation_tracks_index  = NULL;
        obj.user_parameter_count    = 0;
        obj.parameter_id            = NULL;
        obj.parameter_value_length  = NULL;
        obj.parameter_value         = NULL;
        tra.has_component_transform = true;
        tra.translation[0]          = 7;
        tra.translation[1]          = 10;
        tra.translation[2]          = 5;
        tra.scale[0]                = 1;
        tra.scale[1]                = 1;
        tra.scale[2]                = 1;
        tra.orientation_angle       = 0;
        tra.orientation_axis[0]     = 0;
        tra.orientation_axis[1]     = 0;
        tra.orientation_axis[2]     = 0;
        tra.has_general_transform   = false;
        float transform_value[16]   = {1, 0, 0, 0,
                                       0, 1, 0, 0,
                                       0, 0, 1, 0,
                                       0, 0, 0, 1};
        memcpy (tra.transform, transform_value, sizeof(transform_value));
        nod.enable_rendering        = true;
        nod.enable_picking          = true;
        nod.alpha_factor            = 255;
        nod.scope                   = -1;
        nod.has_alignment           = false;
        nod.z_target                = 0;
        nod.y_target                = 0;
        nod.z_reference_index       = 0;
        nod.y_reference_index       = 0;
        lgh.attenuation_constant    = 1;
        lgh.attenuation_linear      = 0;
        lgh.attenuation_quadratic   = 0;
        lgh.color                   = 0x00ffffff;
        lgh.mode                    = M3GLightStruct:: OMNI;
        lgh.intensity               = 1;
        lgh.spot_angle              = 45;
        lgh.spot_exponent           = 0;
        writer->startObject       (M3G_TYPE_LIGHT, &light_index);
        writer->writeObject3D     (obj);
        writer->writeTransformable(tra);
        writer->writeNode         (nod);
        writer->writeLight        (lgh);
        writer->endObject         ();
        cout << ".";
    }

    // M3G_TYPE_WORLD
    {
        M3GObject3DStruct      obj;
        M3GTransformableStruct tra;
        M3GNodeStruct          nod;
        M3GGroupStruct         grp;
        M3GWorldStruct         wld;
        obj.user_id                 = 0;
        obj.animation_tracks_count  = 0;
        obj.animation_tracks_index  = NULL;
        obj.user_parameter_count    = 0;
        obj.parameter_id            = NULL;
        obj.parameter_value_length  = NULL;
        obj.parameter_value         = NULL;
        tra.has_component_transform = true;
        tra.translation[0]          = 0;
        tra.translation[1]          = 0;
        tra.translation[2]          = 0;
        tra.scale[0]                = 1;
        tra.scale[1]                = 1;
        tra.scale[2]                = 1;
        tra.orientation_angle       = 0;
        tra.orientation_axis[0]     = 0;
        tra.orientation_axis[1]     = 0;
        tra.orientation_axis[2]     = 0;
        tra.has_general_transform   = true;
        float transform_value[16]   = {1, 0, 0, 0,
                                       0, 1, 0, 0,
                                       0, 0, 1, 0,
                                       0, 0, 0, 1};
        memcpy (tra.transform, transform_value, sizeof(transform_value));
        nod.enable_rendering        = true;
        nod.enable_picking          = true;
        nod.alpha_factor            = 255;
        nod.scope                   = -1;
        nod.has_alignment           = false;
        nod.z_target                = 0;
        nod.y_target                = 0;
        nod.z_reference_index       = 0;
        nod.y_reference_index       = 0;
        grp.children_index_count    = 3;
        grp.children_index          = new unsigned int[3];
        grp.children_index[0]       = camera_index;
        grp.children_index[1]       = mesh_index;
        grp.children_index[2]       = light_index;
        wld.active_camera_index     = camera_index;
        wld.background_index        = background_index;
        writer->startObject       (M3G_TYPE_WORLD, &world_index);
        writer->writeObject3D     (obj);
        writer->writeTransformable(tra);
        writer->writeNode         (nod);
        writer->writeGroup        (grp);
        writer->writeWorld        (wld);
        writer->endObject         ();
        cout << ".";
    }

    // Section2 : end
    writer->endSection   ();

    cout << "done.\n";
    cout << "  all objects was written successfuly.\n";
    delete writer;

    return 0;
}
